package org.ala.tiktools_clickhouse_generator.generator.entity;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashSet;
import java.util.Set;

import org.ala.tiktools_clickhouse_generator.table.ColumnInfo;
import org.ala.tiktools_clickhouse_generator.tools.NameTools;
import org.ala.tiktools_clickhouse_generator.tools.TypeTools;

/**
 *	字段信息	
 *
 *	@author ala
 *	@date 2025年6月9日
 */
public class FieldInfo {

	
	/**
	 * 	字段对应的列名
	 */
	protected ColumnInfo column;
	/**
	 * 	字段名，get/set名
	 */
	protected String name, upName, desc;
	/**
	 * 	对应的java类型
	 */
	protected Class<?> javaType;
	protected String javaTypeName, javaTypePath;
	/**
	 * 	是否普通java类型
	 */
	protected boolean javaTypeSimple;
	static final Set<Class<?>> SIMPLE_TYPES = new HashSet<>();
	static {
		SIMPLE_TYPES.add(String.class);
		SIMPLE_TYPES.add(Byte.class);
		SIMPLE_TYPES.add(Integer.class);
		SIMPLE_TYPES.add(Long.class);
		SIMPLE_TYPES.add(Float.class);
		SIMPLE_TYPES.add(Double.class);
		SIMPLE_TYPES.add(Boolean.class);
	}
	/**
	 * 	是否是可比较类型
	 */
	protected boolean javaTypeComparable;
	static final Set<Class<?>> COMPARABLE_TYPES = new HashSet<>();
	static {
		COMPARABLE_TYPES.add(Byte.class);
		COMPARABLE_TYPES.add(Integer.class);
		COMPARABLE_TYPES.add(Long.class);
		COMPARABLE_TYPES.add(Float.class);
		COMPARABLE_TYPES.add(Double.class);
		COMPARABLE_TYPES.add(LocalDate.class);
		COMPARABLE_TYPES.add(LocalDateTime.class);
		COMPARABLE_TYPES.add(BigInteger.class);
		COMPARABLE_TYPES.add(BigDecimal.class);
	}
	/**
	 * 	是否是文本类型
	 */
	protected boolean javaTypeText;
	static final Set<Class<?>> TEXT_TYPES = new HashSet<>();
	static {
		COMPARABLE_TYPES.add(String.class);
	}
	
	public static FieldInfo build(ColumnInfo column) {
		FieldInfo fi = new FieldInfo();
		fi.column = column;
		fi.name = NameTools.hump(column.getName());
		fi.upName = NameTools.upFirst(fi.name);
		fi.desc = column.getRemark() == null ? fi.name : column.getRemark();
		
		fi.javaType = TypeTools.typeMapper(column.getType());
		fi.javaTypeName = fi.javaType.getSimpleName();
		fi.javaTypeSimple = SIMPLE_TYPES.contains(fi.javaType);
		fi.javaTypePath = fi.javaType.getPackage().getName() + "." + fi.javaType.getSimpleName();
		fi.javaTypeComparable = COMPARABLE_TYPES.contains(fi.javaType);
		fi.javaTypeText = TEXT_TYPES.contains(fi.javaType);
		return fi;
	}
	
	
	public ColumnInfo getColumn() {
		return column;
	}
	public void setColumn(ColumnInfo column) {
		this.column = column;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getUpName() {
		return upName;
	}
	public void setUpName(String upName) {
		this.upName = upName;
	}
	public boolean isJavaTypeComparable() {
		return javaTypeComparable;
	}
	public void setJavaTypeComparable(boolean javaTypeComparable) {
		this.javaTypeComparable = javaTypeComparable;
	}
	public boolean isJavaTypeText() {
		return javaTypeText;
	}
	public void setJavaTypeText(boolean javaTypeText) {
		this.javaTypeText = javaTypeText;
	}
	public Class<?> getJavaType() {
		return javaType;
	}
	public void setJavaType(Class<?> javaType) {
		this.javaType = javaType;
	}
	public String getJavaTypeName() {
		return javaTypeName;
	}
	public void setJavaTypeName(String javaTypeName) {
		this.javaTypeName = javaTypeName;
	}
	public boolean isJavaTypeSimple() {
		return javaTypeSimple;
	}
	public void setJavaTypeSimple(boolean javaTypeSimple) {
		this.javaTypeSimple = javaTypeSimple;
	}
	public String getJavaTypePath() {
		return javaTypePath;
	}
	public void setJavaTypePath(String javaTypePath) {
		this.javaTypePath = javaTypePath;
	}
	public String getDesc() {
		return desc;
	}
	public void setDesc(String desc) {
		this.desc = desc;
	}
}
